/**
 * 系统管理 书签管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>书签管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
<!--        <el-select size="small" v-model="formInline.isLock" placeholder="请选择">-->
<!--          <el-option label="全部" value=""></el-option>-->
<!--          <el-option label="正常" value="N"></el-option>-->
<!--          <el-option label="已锁定" value="Y"></el-option>-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.userName" placeholder="输入书签名字"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.userMobile" placeholder="输入书签用户"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="BookMarkData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="title" label="标题" width="100">
      </el-table-column>
      <el-table-column align="center" sortable prop="url" label="连接URL" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="urlgw" disabled="true" label="官网" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="description" label="描述" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="zcount" label="点赞数" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="createTime" label="添加时间" min-width="100">
      </el-table-column>
<!--     -->
<!--      <el-table-column align="center" sortable prop="userRole" label="状态" min-width="50">-->
<!--        <template slot-scope="scope">-->
<!--          <el-switch v-model="scope.row.isLock=='0'?nshow:fshow" active-color="#13ce66" inactive-color="#ff4949" @change="editType(scope.$index, scope.row)">-->
<!--          </el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->


      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.sqid)" type="success" plain>编辑</el-button>
          <el-button @click="dele(scope.row.sqid)" size="mini" type="danger" plain>删除</el-button>
        </template>
      </el-table-column>

    </el-table>

<!--    <div class="pagination-container">-->
<!--      <el-pagination-->
<!--        class="pagiantion"-->
<!--        @size-change="fetchData"-->
<!--        @current-change="fetchData"-->
<!--        :current-page.sync="currentPage"-->
<!--        :page-sizes="[3, 5, 10, 40]"-->
<!--        :page-size="pageSize"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--        :total="total">-->
<!--      </el-pagination>-->
<!--    </div>-->

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px"  :model="pojoData" :rules="rules" ref="pojoData">
        <el-input type="hidden" v-model="pojoData.sqid"></el-input>
        <el-form-item label="标题" prop="title">
          <el-input size="small" v-model="pojoData.title"   auto-complete="off"   placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="连接" prop="url">
          <el-input size="small" v-model="pojoData.url"  auto-complete="off"   placeholder="请输入连接"></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="description">
          <el-input size="small" v-model="pojoData.description" auto-complete="off" placeholder="描述"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="save()">保存</el-button>
      </div>
    </el-dialog>





  </div>
</template>










<script>
// 导入请求方法
import {
  userList,
  userSave,
  userDelete,
  userPwd,
  userExpireToken,
  userFlashCache,
  userLock,
  UserDeptTree,
  UserDeptSave,
  UserDeptdeptTree,
  UserChangeDept
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
 export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      BookMarkData:[],
      pojoData:{},
      currentPage: 1, //第几页
      pageSize:10,// 分页
      total:0,
      imageface:'',
      title: '编辑用户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示


      // 选择数据
      selectdata: [],
      // rules表单验证
      rules: {
        // userName: [
        //   { required: true, message: '请输入用户名', trigger: 'blur' }
        // ],
        // userRealName: [
        //   { required: true, message: '请输入姓名', trigger: 'blur' }
        // ],
        // roleId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        // userMobile: [
        //   { required: true, message: '请输入手机号', trigger: 'blur' },
        //   {
        //     pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
        //     required: true,
        //     message: '请输入正确的手机号',
        //     trigger: 'blur'
        //   }
        // ],
        // userEmail: [
        //   { required: true, message: '请输入邮箱', trigger: 'blur' },
        //   {
        //     pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
        //     required: true,
        //     message: '请输入正确的邮箱',
        //     trigger: 'blur'
        //   }
        // ],
        // userSex: [{ required: true, message: '请选择性别', trigger: 'blur' }]
      },
      // 删除用户
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      // 重置密码
      resetpsd: {
        userId: '',
        token: localStorage.getItem('logintoken')
      },

      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
        deptId: '',
        userName: '',
        userMobile: '',
        isLock: ''
      },
      //用户数据
      // userData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      // pageparm: {
      //   currentPage: 1,
      //   pageSize: 3,
      //   total: 10
      // }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕 就显示
   */
  created(){
    this.templateId= this.$route.query.id
    this.searchMap={ template_id: this.$route.query.id}
    this.fetchData();
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {

    //全部显示
    fetchData (){
    this.$axios.get(`/api/bookmark/queryAllList?pageNo=${this.currentPage}&pageSize=${this.pageSize}`,this.searchMap).then(response => {
      this.BookMarkData = response.data.list;
      //console.log(this.BookMarkData)
      this.total = response.data.total;

    });
    },
    //显示编辑界面
    //显示编辑界面
    // handleEdit: function(index, row) {
    //   this.editFormVisible = true
    //     this.title = '修改1'
    //     this.pojo.userName = row.userName
    //     this.pojo.password = row.password
    //     this.pojo.nickName = row.nickName
    // },

    //编辑界面

    handleEdit(id){
      var _this=this;
      // 打开窗口
      _this.editFormVisible = true;
      // 调用查询
      _this.$axios.get(`/api/bookmark/queryAllList?sqid=${id}`).then((response) =>{
        _this.pojoData= response.data.list[0];
      })
    },

    save(){
      var _this = this;
      // this.pojo.image= this.imageUrl; //如页面有图片上传功能放开注释
      this.$axios.post(`/api/bookmark/${this.pojoData.sqid==null?'insertSelective':'updateBookMark'}`,_this.pojoData).then(response => {
        if (response.status == 200 && response.data.status == 'success') {
          _this.$message({type: 'success', message: '操作成功!'})
          _this.fetchData();
          this.editFormVisible = false ;//关闭窗口
          return;
        }
        _this.$message({type: 'error', message: '操作失败!'})
      });
    },

    //搜索事件
    search() {
      this.getdata(this.formInline)
    },
    // 修改type
    editType: function(index, row) {
      this.loading = true
      let parm = {
        lock: '',
        userId: '',
        token: localStorage.getItem('logintoken')
      }
      parm.userId = row.userId
      let lock = row.isLock
      if (lock == 'N') {
        parm.lock = 'Y'
      } else {
        parm.lock = 'N'
      }
      // 修改状态
      userLock(parm).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.$message({
            type: 'success',
            message: '状态修改成功'
          })
          this.getdata(this.formInline)
        }
      })
    },

    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          userSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '数据保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },

    handleClick(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([])
        this.$refs.tree.setCheckedNodes([data])
        this.unitparm.deptId = data.id
        this.unitparm.deptName = data.name
        //交叉点击节点
      } else {
      }
    },

    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },

    dele(id){
      var _this = this;
      this.$confirm('删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.get(`/api/bookmark/deleteByPrimaryKey/${id}`).then(response => {
          if (response .status == 200 && response .data.status == 'success') {
            _this.$message({type: 'success', message: '删除成功!'})
            _this.fetchData();
            return;
          }
          _this.$message({type: 'error', message: '删除失败!'})
        }, response => {

          _this.$message({type: 'error', message: '删除失败!'})
        });
      }).catch(() => {
        _this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    //数据格式化
    changeArr(data) {
      var pos = {}
      var tree = []
      var i = 0
      while (data.length != 0) {
        if (data[i].pId == 0) {
          tree.push({
            id: data[i].id,
            name: data[i].name,
            pId: data[i].pId,
            open: data[i].open,
            checked: data[i].checked,
            children: []
          })
          pos[data[i].id] = [tree.length - 1]
          data.splice(i, 1)
          i--
        } else {
          var posArr = pos[data[i].pId]
          if (posArr != undefined) {
            var obj = tree[posArr[0]]
            for (var j = 1; j < posArr.length; j++) {
              obj = obj.children[posArr[j]]
            }

            obj.children.push({
              id: data[i].id,
              name: data[i].name,
              pId: data[i].pId,
              open: data[i].open,
              checked: data[i].checked,
              children: []
            })
            pos[data[i].id] = posArr.concat([obj.children.length - 1])
            data.splice(i, 1)
            i--
          }
        }
        i++
        if (i > data.length - 1) {
          i = 0
        }
      }
      return tree
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.pagination-container{

  margin-top:5px;
  margin-left: -10px;

  }
</style>

